vim.g.mapleader = " "

local keymap = vim.keymap

-- ------------插入模式-------------- --
keymap.set("i", "jk", "<ESC>")

-- ------------视觉模式-------------- --
-- 单行或多行移动
keymap.set("v", "J", ":m '>+1<CR>gv=gv'")
keymap.set("v", "K", ":m '<-2<CR>gv=gv'")

-- -------------正常模式-------------- --
-- 窗口
keymap.set("n", "<leader>sv", "<C-w>v")
keymap.set("n", "<leader>sh", "<C-w>s")

-- 取消高亮
keymap.set("n", "<leader>nh", ":nohl<CR>")
keymap.set("n", "ss", ":w<cr>")
keymap.set("n", "sq", ":wq<cr>")
keymap.set("n", "qq", ":q<cr>")

-- buffer
keymap.set("n", "<C-h>", ":bprev<cr>")
keymap.set("n", "<C-l>", ":bnext<cr>")

-- nvim-tree
keymap.set("n", "<leader>e", ":NvimTreeToggle<CR>")
keymap.set("n", "<leader>nf", ":NvimTreeNewFile<CR>")
keymap.set("n", "<leader>nd", ":NvimTreeNewFolder<CR>")

local builtin = require('telescope.builtin')
vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
vim.keymap.set('n', '<leader>fg', builtin.live_grep, {})
vim.keymap.set('n', '<leader>fb', builtin.buffers, {})
vim.keymap.set('n', '<leader>fh', builtin.help_tags, {})

-- lspsaga
keymap.set('n', 'gd', ':Lspsaga goto_definition<CR>', {silent = true})
keymap.set('n', '<leader>fd', ':Lspsaga peek_definition<CR>', {silent = true})
keymap.set('n', '<leader>fi', ':Lspsaga finder<CR>', {silent = true})
keymap.set('n', '<leader>ca', ':Lspsaga code_action<CR>', {silent = true})

-- Trouble
vim.keymap.set("n", "<leader>xx", function() require("trouble").toggle() end)
vim.keymap.set("n", "<leader>xw", function() require("trouble").toggle("workspace_diagnostics") end)
vim.keymap.set("n", "<leader>xd", function() require("trouble").toggle("document_diagnostics") end)
vim.keymap.set("n", "<leader>xq", function() require("trouble").toggle("quickfix") end)
vim.keymap.set("n", "<leader>xl", function() require("trouble").toggle("loclist") end)
vim.keymap.set("n", "gR", function() require("trouble").toggle("lsp_references") end)
